package cn.edu.lingnan.controllerV1;

import cn.edu.lingnan.mongodb.SearchHistory;
import cn.edu.lingnan.service.PaperService;
import cn.edu.lingnan.user.dtos.AddPaperQuestionDto;
import cn.edu.lingnan.user.dtos.ListPaperDto;
import cn.edu.lingnan.user.dtos.ListQuestionDto;
import cn.edu.lingnan.user.dtos.Result;
import cn.edu.lingnan.user.pojos.PageBean;
import cn.edu.lingnan.user.pojos.Paper;
import cn.edu.lingnan.user.pojos.Question;
import cn.edu.lingnan.user.pojos.QuestionStore;
import cn.edu.lingnan.user.vos.PaperWithQuestionVo;
import cn.edu.lingnan.user.vos.StoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/paper")
@Slf4j
public class PaperController {

    @Autowired
    private PaperService paperService;

    /**
     * 分页查询试卷列表
     * @param listPaperDto
     * @return
     */
    @PostMapping("/listPaper")
    private Result<?> list(@RequestBody ListPaperDto listPaperDto) {
        PageBean<?> pageBean = paperService.list(listPaperDto);
        return Result.success(pageBean);
    }

    /**
     * 新增试卷
     * @param paper
     * @return
     */

    @PostMapping("/addPaper")
    private Result addPaper(@RequestBody Paper paper) {
         paperService.addPaper(paper);
        return Result.success();
    }

    /**
     * 删除试卷
     * @param
     * @return
     */
    @DeleteMapping("/deletePaper")
    private Result deletePaper(@RequestParam("paperId") Integer paperId) {
        Integer result = paperService.deletePaper(paperId);
        if(result==-1){
            //试卷关联考试，不能删除
            return Result.error("试卷关联考试，不能删除");
        }
        return Result.success();
    }

    /**
     * 根据试卷id查询大致试卷信息
     * @param paperId
     * @return
     */

    @GetMapping("/getPaperById")
    private Result getPaperById(@RequestParam("paperId") Integer paperId) {
        Paper paper = paperService.getPaperById(paperId);
        return Result.success(paper);
    }

    /**
     * 更新试卷信息
     * @param paper
     * @return
     */

    @PostMapping("/updatePaper")
    private Result updatePaper( @RequestBody Paper paper) {
        paperService.updatePaper(paper);
        return Result.success();
    }
    /**
     * 根据试卷id查询试卷及试题信息
     * @param paperId
     * @return
     */

    @GetMapping("/paper_detail")
    public Result<?> paper_detail(@RequestParam("paperId") Integer paperId) {
        PaperWithQuestionVo paperWithQuestionVo = paperService.getPaperWithQuestionVo(paperId);
        return Result.success(paperWithQuestionVo);
    }

    /**
     * 获取试题库列表
     * @param userId
     * @return
     */
    @GetMapping("/get_question_bank_list")
    public Result<?> getQuestionBankList(@RequestParam("userId") Integer userId) {
        List<StoreVo> questionStores = paperService.getQuestionBankList(userId);
        return Result.success(questionStores);
    }

    /**
     * 查询试题列表
     * @param listQuestionDto
     * @return
     */
    @PostMapping("/get_question_list")
    private Result<PageBean<Question>> getQuestionList(@RequestBody ListQuestionDto listQuestionDto) {
        if (listQuestionDto.getUserId() == null) {
            return Result.error("用户id不能为空");
        }
        PageBean<Question> pageBean = paperService.getQuestionList(listQuestionDto);
        return Result.success(pageBean);
    }
    /**
     * 获取搜索记录
     */
    @GetMapping("/get_search_history")
    public Result<List<SearchHistory>> getRecentSearches() {
        List<SearchHistory> searchHistory = paperService.getRecentSearches();
        return Result.success(searchHistory);
    }
    /**
     * 删除搜索记录
     *  @param id
     */
    @DeleteMapping("/delete_search_history/{id}")
    public Result<String> deleteSearchHistory(@PathVariable String id) {
        paperService.deleteSearchHistory(id);
        return Result.success("删除成功");
    }


    /**
     * 新增试卷试题
     * @param addPaperQuestionDto
     * @return
     */

    @PostMapping("/add_paper_question")
    private Result addPaperQuestion(@RequestBody AddPaperQuestionDto addPaperQuestionDto) {
        try {
            paperService.addPaperQuestion(addPaperQuestionDto);
            return Result.success("插入成功");
        } catch (RuntimeException e) {
            // 这里捕获 ServiceImpl 抛出的 RuntimeException
            log.error("添加试题失败: {}", e.getMessage(), e);
            return Result.error("添加试题失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/delete_paper_question")
    private Result deletePaperQuestion(@RequestParam("paperId") Integer paperId,
                                       @RequestParam("questionId") Integer questionId,
                                       @RequestParam( value = "examId", required = false) Integer examId) {
       Boolean flag = paperService.deletePaperQuestion(paperId, questionId, examId);
        if(flag){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败,试卷关联已发布考试");

    }
     }







}
